package cn.player.media.timeshift;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import cn.player.R;
import cn.player.media.entity.LiveEpg;
import cn.player.media.util.DateUtil;

public class TimeShiftEPG {
    private Context context;
    private LinearLayout llEpgLayout;
    private TextView tvEpg;
    private LinearLayout.LayoutParams epgTextParams;
    /**
     * 2014-1-2 增加点击回调函数
     **/
    private OnClickListener callBack;

    private long beginTime, endTime;
    private TimeShiftParams rParams;

    private int bgColor = 0;

    private int black, dkgray;

    private int llEpgHeight;

    public TimeShiftEPG(Context context, LinearLayout epg) {
        this.context = context;
        this.llEpgLayout = epg;
    }

    public void create(Map<String, LiveEpg> channels, TimeShiftParams r, long begin, long end, OnClickListener callBack) {
        this.rParams = r;
        this.beginTime = begin;
        this.endTime = end;
        this.callBack = callBack;
        black = Color.parseColor("#000000");
        dkgray = Color.parseColor("#6e6f6e");
        addEPG(channels);
    }

    public void clear(TimeShiftParams tsParams) {
        this.rParams = tsParams;
        llEpgLayout.removeAllViewsInLayout();
        llEpgLayout.invalidate();
        fixHeight();
    }

    private void addEPG(Map<String, LiveEpg> data) {

        llEpgLayout.removeAllViewsInLayout();
        llEpgLayout.setVisibility(View.VISIBLE);
        fixHeight();
        bgColor = black;
        if (data == null) {
            return;
        }
        long bTime = beginTime / 1000;
        long eTime = endTime / 1000;
        long oldStart = 0, oldEnd = 0;

        String sbTime = DateUtil.getTimeString(beginTime, "yyyyMMdd");
        LiveEpg epgs = data.get(sbTime);
        List<LiveEpg.Program> programs = epgs.getProgram();

        for (int i = 0; i < programs.size(); i++) {
            LiveEpg.Program bean = programs.get(i);
            String title = bean.getT();
            long start = bean.getSt();
            long end = bean.getEt();
            long duration = bean.getDuration();
            String showTime = bean.getShowTime();

            long differ = 0;
            if (end < eTime) {
                if (oldStart == 0) {
                    differ = start - bTime;
                } else {
                    differ = start - oldEnd;
                }

                //如果duration<0,通过后一个节目的开始时间计算得出
                if (duration < 0) {
                    differ = 0;
                    if (i == programs.size() - 1) {
                        end = (int) eTime;
                    } else {
                        LiveEpg.Program _bean = programs.get(i + 1);
                        end = _bean.getSt();
                    }
                    duration = (int) (end - start);
                }
                oldStart = start;
                oldEnd = end;
                if (differ > 0) {
                    createText((int) differ, "");
                }
                createText(duration, showTime + " " + title);
            } else {
                differ = eTime - oldStart;
                if (differ > 0) {
                    createText((int) differ, "");
                }
            }
        }
        llEpgLayout.measure(0, 0);
        llEpgHeight = llEpgLayout.getMeasuredHeight();
    }

    /**
     * 隐藏列表保持宽度不变
     */
    private void fixHeight() {
        if (llEpgHeight != 0) {
            int width = (int) (rParams.secondToCanvas / rParams.dpToDate);
            LinearLayout.LayoutParams epgParames = new LinearLayout.LayoutParams(width, llEpgHeight);
            llEpgLayout.setLayoutParams(epgParames);
        }
    }

    /**
     * 创建epg信息
     *
     * @param durationTime 每个节目持续时长
     * @param displayText  epg节目名称
     */
    private void createText(long durationTime, String displayText) {
        int width = (int) (durationTime / rParams.dpToDate);
        epgTextParams = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
        epgTextParams.gravity = Gravity.CENTER;
        tvEpg = new TextView(context);
        tvEpg.setTextColor(Color.WHITE);
        tvEpg.setBackgroundResource(R.drawable.bg_epg_text);
        tvEpg.getBackground().setAlpha(100);
        bgColor = bgColor == black ? dkgray : black;
        tvEpg.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        tvEpg.setTextSize(16);
        tvEpg.setEllipsize(TruncateAt.MARQUEE);
        tvEpg.setMarqueeRepeatLimit(-1);
        tvEpg.setSingleLine(true);
        tvEpg.setFocusable(true);
        tvEpg.setFocusableInTouchMode(true);
        tvEpg.setPadding(10, 5, 10, 5);
        tvEpg.setText(displayText);
        tvEpg.setLayoutParams(epgTextParams);
        tvEpg.setOnClickListener(callBack);
        llEpgLayout.addView(tvEpg);
    }
}
